﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace D.AT
{
    public partial class FrmSpcCheckMessage : Form
    {
        public string msg = string.Empty;
        public string result = string.Empty;
        public FrmSpcCheckMessage(string _msg, string result)
        {
            InitializeComponent();
            this.msg = _msg;
            this.result = result;
        }

        private void FrmSpcCheckMessage_Load(object sender, EventArgs e)
        {

            try
            {
                //JObject.ToString()方法会内部调用格式化，所以直接使用即可
                //判读是数组还是对象
                if (msg != null && msg.Length > 0)
                {
                    if (msg.Trim().StartsWith("["))
                    {
                        JArray jobj = JArray.Parse(msg);
                        txtMsg.Text = jobj.ToString();
                    }
                    else if (msg.Trim().StartsWith("{"))
                    {
                        JObject jobj = JObject.Parse(msg);
                        txtMsg.Text = jobj.ToString();
                    }
                }

                if (result != null && result.Length > 0)
                {
                    if (result.Trim().StartsWith("["))
                    {
                        JArray jobj = JArray.Parse(result);
                        txtResult.Text = jobj.ToString();
                    }
                    else if (result.Trim().StartsWith("{"))
                    {
                        JObject jobj = JObject.Parse(result);
                        txtResult.Text = jobj.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误的JSON");
            }
        }
    }
}
